//
//  ZZLabel.m
//  ZhiYiBao
//
//  Created by czz on 2017/3/1.
//  Copyright © 2017年 athena. All rights reserved.
//

#import "ZZLabel.h"

@implementation ZZLabel{
    CGRect defaultFrame;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

-(BOOL) isObjNull:(NSObject *) obj{
    if (obj == NULL) {
        return YES;
    }
    if (obj == nil) {
        return YES;
    }
    if (obj == [NSNull class]) {
        return YES;
    }
    if (obj == [NSNull null]) {
        return YES;
    }
    return NO;
}
- (BOOL) isBlankString:(NSString *)string {
    if ([self isObjNull:string]) {
        return YES;
    }
    if ([[[NSString stringWithFormat:@"%@",string] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]] length]==0) {
        return YES;
    }
    return NO;
}
-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        defaultFrame = frame;
    }
    return self;
}
-(void)setText:(NSString *)text{
    [super setText:text];
    BOOL viewIsChanged = NO;
    if (_isChangeHeightWithSetText == YES) {
        viewIsChanged = YES;
        [self changeFrameWithHeight];
    }
    if (_isChangeWidthWithSetText == YES) {
        viewIsChanged = YES;
        [self changeFrameWithWidth];
    }
    if (viewIsChanged) {
        if (_viewChangedBlock) {
            _viewChangedBlock(self);
        }
        if ([_delegate respondsToSelector:@selector(zzLabelViewChanged:)]) {
            [_delegate zzLabelViewChanged:self];
        }
    }
}
-(void)changeFrameWithHeight{
    if ([self isBlankString:self.text]) {
        CGRect selfFrame = self.frame;
        selfFrame.size.height = 0;
        [self setFrame:selfFrame];
        return;
    }
    CGSize labelSize = [NSString getSizeToString:self.text forFont:self.font constrainedToSize:CGSizeMake((_maxWidth == 0) ? defaultFrame.size.width : _maxWidth, (_maxHeight == 0) ? MAXFLOAT : _maxHeight) lineBreakMode:self.lineBreakMode];
    CGRect selfFrame = self.frame;
    selfFrame.size.height = labelSize.height + _moreHeightWillChange;
    [self setFrame:selfFrame];
}
-(void)changeFrameWithWidth{
    if ([self isBlankString:self.text]) {
        CGRect selfFrame = self.frame;
        selfFrame.size.width = 0;
        [self setFrame:selfFrame];
        return;
    }
    CGSize labelSize = [NSString getSizeToString:self.text forFont:self.font constrainedToSize:CGSizeMake((_maxWidth == 0) ? MAXFLOAT : _maxWidth, (_maxHeight == 0) ? defaultFrame.size.height : _maxHeight) lineBreakMode:self.lineBreakMode];
    CGRect selfFrame = self.frame;
    selfFrame.size.width = labelSize.width + _moreWidthWillChange;
    [self setFrame:selfFrame];
}
@end
